package com.example.hao.eventdistribution;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class MyFrameLayout extends FrameLayout {
    public MyFrameLayout(@NonNull Context context) {
        super(context);
    }

    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private static final String TAG = "MainActivity";

    /**
     * 事件消费
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.w(TAG, "onTouchEvent: " + getTag());
        return super.onTouchEvent(event);

    }

    /**
     * 事件分发
     *
     * @param ev
     * @return true :表示终止整条事件链的传递; false:只终止该ViewGroup的子View的分发
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.w(TAG, "dispatchTouchEvent: " + getTag());
//        switch (getTag().toString()) {
//            case "parent":
////                return true;
//                break;
//            case "parent_child3":
//                break;
//        }
        return super.dispatchTouchEvent(ev);
    }


    /**
     * 事件拦截(是否拦截子View的事件分发)
     *
     * @param ev
     * @return false 表示不拦截事件;false:类似于在 dispatchTouchEvent 返回了false一样的效果
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.w(TAG, "onInterceptTouchEvent: " + getTag());
        switch (getTag().toString()) {
//            case "parent":
//                return true;
//            case "parent_child3":
//                return true;
        }
        return false;//默认 返回 false
    }
}
